const host = process.env.API_GUO_URL
Promise.prototype.finally = function (callback) {
    var Promise = this.constructor;
    return this.then(
        function (value) {
            Promise.resolve(callback()).then(
                function () {
                    return value;
                }
            );
        },
        function (reason) {
            Promise.resolve(callback()).then(
                function () {
                    throw reason;
                }
            );
        }
    );
}
function request(url, method, data) {
    // var localtoken = ls.get('wx-token')
    var localtoken = wx.getStorageSync('tokens')
    //   ls.set('wx-token', localtoken)
    var headers = {
        'content-type': 'application/json',
        // 'Api-Version': '2.2.0'
    }
    if (localtoken) {
        headers['accessToken'] = localtoken
    }
    // wx.showLoading({
    //     title: '加载中' // 数据请求前loading
    // })
    return new Promise((resolve, reject) => {
        wx.request({
            url: url, // 仅为示例，并非真实的接口地址
            method: method,
            data: data,
            header: headers,
            success: function (res) {
                if (res.data.code === '11') {
                    wx.clearStorage()
                    wx.reLaunch({ url: '../login/main' })
                }
                resolve(res.data)
            },
            fail: function (res) {
                // wx.hideLoading()
                // reject(false)
            },
            complete: function () {
                wx.hideLoading()
            }
        })
    })
}
function get(url, pargm) {
    return request(url, 'GET', pargm)
}
function post(url, pargm) {
    return request(url, 'POST', pargm)
}
export default {
    request,
    get,
    post,
    host
}
